﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using MS.InformationManagement.Presenter.Contacts;
using MS.InformationManagement.Support;
using MS.InformationManagement.DAL.DocumentCenter;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Presenter.Support;
using System.Web.UI;

namespace MS.InformationManagement.WP.Layouts.MS.InformationManagement.WP
{
    public partial class ContactsManager : BaseApplicationPage, IContactsView
    {

        #region fields

        ContactosPresenter _presenter;

        #endregion

        #region Properties

        public DAL.DocumentCenter.Contactos ContactsEntity { get; set; }

        public int? SelectedContactId
        {
            get 
            {
                if (string.IsNullOrEmpty(Page.Request.QueryString["ID"]))
                    return null;
                else
                    return int.Parse(Page.Request.QueryString["ID"]);
            }
        }

        #endregion

        #region Form Events

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                _presenter = new ContactosPresenter(this);
                if (IsPostBack) return;
                _presenter.HandleLoad();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void ChangeAction(object sender, EventArgs e)
        {
            try
            {
                InitControls();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void Action_Save(object sender, EventArgs e)
        {
            try
            {
                GetContact();
                if (SelectedContactId.HasValue)
                    _presenter.HandleUpdate();
                else
                    _presenter.HandleSave();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void Action_Cancel(object sender, EventArgs e)
        {
            if (Request.QueryString["Dlg"] != "1")
                this.CloseWindow();
            else
            {
                this.Page.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
                this.Page.Response.Flush();
                this.Page.Response.End();
            }

        }

        protected override void OnInit(EventArgs e)
        {
            try
            {
                base.OnInit(e);
                Page.RegisterAjaxLoaderPlugin();
                ddlTipoContacto.AddLoader();
                txtId.Required(ExceptionConstants.ERROR_CAMPO_NULO, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
                txtId.RegularExpression(RegularExpresions.EXP_ALFANUMERIC_NO_TILDE, ExceptionConstants.ERROR_INVALID_CHARACTERS, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
                txtAddress.Required(ExceptionConstants.ERROR_CAMPO_NULO, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
                //txtTelefono.Required(ExceptionConstants.ERROR_CAMPO_NULO, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
                txtTelefono.RegularExpression(RegularExpresions.EXP_ID,ExceptionConstants.ERROR_INVALID_CHARACTERS, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
                //txtEmail.Required(ExceptionConstants.ERROR_CAMPO_NULO, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
                txtEmail.RegularExpression(RegularExpresions.EXP_EMAIL, ExceptionConstants.ERROR_INVALID_EMAIL, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
                if (IsPostBack || SelectedContactId.HasValue) return;
                InitControls();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        #endregion

        #region Implemented Methods

        public void Show()
        {
            if (!SelectedContactId.HasValue) return;
            if (this.ContactsEntity.GetType().Name == typeof(ContactosExternosPersonas).Name)
                this.setPeople();
            else
                this.setEntidad();
            ddlTipoContacto.Enabled = false;
            InitControls();
        }

        public void MessageClearFields(string message,bool clearControls)
        {
            if (clearControls)
                clearFields();
            HandleSuccess(message, null);
        }

        #endregion

        #region Private Methods

        private void InitControls()
        {
            bool peopleMode = true;
            if (ddlTipoContacto.SelectedValue == "2")
            {
                peopleMode = false;
                lblId.Text = "Nit *";
                txtBusinessName.Required(ExceptionConstants.ERROR_CAMPO_NULO, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
                txtBusinessName.RegularExpression(RegularExpresions.EXP_ALFANUMERIC_NO_TILDE_NUMBERS, ExceptionConstants.ERROR_INVALID_CHARACTERS, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
                txtRepresented.Required(ExceptionConstants.ERROR_CAMPO_NULO, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
                txtRepresented.RegularExpression(RegularExpresions.EXP_ALFANUMERIC_NO_TILDE_NO_NUMBERS, ExceptionConstants.ERROR_INVALID_CHARACTERS, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
                //txtSiglas.Required(ExceptionConstants.ERROR_CAMPO_NULO, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
            }
            else
            {
                txtNames.Required(ExceptionConstants.ERROR_CAMPO_NULO, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
                txtNames.RegularExpression(RegularExpresions.EXP_ALFANUMERIC_NO_TILDE_NO_NUMBERS, ExceptionConstants.ERROR_INVALID_CHARACTERS, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
                txtFirstName.Required(ExceptionConstants.ERROR_CAMPO_NULO, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
                txtFirstName.RegularExpression(RegularExpresions.EXP_ALFANUMERIC_NO_TILDE_NO_NUMBERS, ExceptionConstants.ERROR_INVALID_CHARACTERS, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
                //txtApellido.Required(ExceptionConstants.ERROR_CAMPO_NULO, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
                txtApellido.RegularExpression(RegularExpresions.EXP_ALFANUMERIC_NO_TILDE_NO_NUMBERS, ExceptionConstants.ERROR_INVALID_CHARACTERS, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
            }

            divNames.Visible = peopleMode;
            divFirstName.Visible = peopleMode;
            divLastName.Visible = peopleMode;
            divBusinessName.Visible = !peopleMode;
            divRepresented.Visible = !peopleMode;
            divSiglas.Visible = !peopleMode;
        }

        private void GetContact()
        {
            if (ddlTipoContacto.SelectedValue == "1")
                ContactsEntity = GetPeople();
            else
                ContactsEntity = GetEntity();
        }

        private Personas GetPeople()
        {
            Personas tempEntity = new Personas();
            if (SelectedContactId.HasValue)
                tempEntity.Id = SelectedContactId.Value;
            tempEntity.Title = txtNames.Text + " " + txtFirstName.Text + " " + txtApellido.Text;
            tempEntity.ID = txtId.Text;
            tempEntity.Nombres = txtNames.Text;
            tempEntity.PrimerApellido = txtFirstName.Text;
            tempEntity.SegundoApellido = txtApellido.Text;
            tempEntity.Dirección = txtAddress.Text;
            tempEntity.Teléfono = txtTelefono.Text;
            tempEntity.Email = txtEmail.Text;
            tempEntity.TipoDeContacto = "Ciudadano";
            return tempEntity;
        }


        private Entidades GetEntity()
        {
            Entidades tempEntity = new Entidades();
            if (SelectedContactId.HasValue)
                tempEntity.Id = SelectedContactId.Value;
            tempEntity.ID = txtId.Text;
            tempEntity.Title = txtBusinessName.Text;
            tempEntity.RazónSocial = txtBusinessName.Text;
            tempEntity.Representante = txtRepresented.Text;
            tempEntity.Sigla = txtSiglas.Text;
            tempEntity.Dirección = txtAddress.Text;
            tempEntity.Teléfono = txtTelefono.Text;
            tempEntity.Email = txtEmail.Text;
            tempEntity.TipoDeContacto = "Entidad";
            return tempEntity;
        }

        private void setPeople()
        {
            Personas currentContact = this.ContactsEntity as Personas;
            ddlTipoContacto.SelectedIndex = 0;
            txtNames.Text = currentContact.Nombres;
            txtFirstName.Text = currentContact.PrimerApellido;
            txtApellido.Text = currentContact.SegundoApellido;
            txtId.Text = currentContact.ID;
            txtAddress.Text = currentContact.Dirección;
            txtTelefono.Text = currentContact.Teléfono;
            txtEmail.Text = currentContact.Email;
        }

        private void setEntidad()
        {
            Entidades currentEntidades = this.ContactsEntity as Entidades;
            ddlTipoContacto.SelectedIndex = 1;
            txtId.Text = currentEntidades.ID;
            txtBusinessName.Text = currentEntidades.RazónSocial;
            txtRepresented.Text = currentEntidades.Representante;
            txtSiglas.Text = currentEntidades.Sigla;
            txtAddress.Text = currentEntidades.Dirección;
            txtTelefono.Text = currentEntidades.Teléfono;
            txtEmail.Text = currentEntidades.Email;
        }

        private void clearFields()
        {
            txtId.Text = string.Empty;
            txtBusinessName.Text = string.Empty;
            txtBusinessName.Text = string.Empty;
            txtRepresented.Text = string.Empty;
            txtSiglas.Text = string.Empty;
            txtAddress.Text = string.Empty;
            txtTelefono.Text = string.Empty;
            txtEmail.Text = string.Empty;
            txtNames.Text = string.Empty;
            txtFirstName.Text = string.Empty;
            txtApellido.Text = string.Empty;
        }

        #endregion

    }
}
